/**
  * Created by wyc on 6/13/2017.
  */
package ninenineqs

object p09 {
  def pack_v1[T](input:List[T]):List[List[T]] = {
    input.foldLeft(List[List[T]]()) {
      case (Nil, x) => List(List(x))
      case (lst, x) if (lst.last.head == x) => lst.init :+ ( lst.last :+ x )
      case (lst, x) => lst :+ List(x)
    }
  }
  def pack_v2[T](input:List[T]):List[List[T]] = {
    input match {
      case Nil => Nil
      case lst => lst.takeWhile(_ == lst.head ) ::  pack_v2(lst.dropWhile(_ == lst.head ))
    }
  }
  def pack_v3[T](input:List[T]):List[List[T]] = {
    input.span(x => x == input.head) match {
      case (Nil, Nil) => Nil
      case (xs, ys) => xs :: pack_v3(ys)
    }
  }
}
